package demo.practice.leetcode_eveyday;

public class P135 {


    public int candy(int[] ratings) {
        int len = ratings.length;
        int[] l = new int[len];

        l[0] = 1;
        for (int i = 1; i < ratings.length; i++) {
            if (ratings[i] > ratings[i - 1]) {
                l[i] = l[i - 1] + 1;
            } else {
                l[i] = 1;
            }
        }

        int ret = l[len - 1];
        int right = 1;
        for (int i = len - 2; i >= 0; i--) { //len-2是从倒数第二个开始
            if (ratings[i] > ratings[i + 1]) {
                right++;
            } else {
                right = 1;
            }
            ret += Math.max(l[i], right);
        }
        return ret;
    }
}
